package com.javalong.affairs.module.renwujinzhang.ui;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView;

import com.javalong.affairs.R;

/**
 * Created by 杨观回 on 2016/2/28 0028.
 */
public class ImageEditView extends FrameLayout implements UIValue {

    private ImageView iv_left;
    private EditText et_right;

    private LayoutInflater mInflater;

    public ImageEditView(Context context) {
        super(context);
        init();
    }

    public ImageEditView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.ImageEdit);
        int n = typedArray.getIndexCount();
        for (int i = 0; i < n; i++) {
            int attr = typedArray.getIndex(i);
            int resId = 0;
            switch (attr) {
                case R.styleable.ImageEdit_ie_left_image:
                    resId = typedArray.getResourceId(R.styleable.ImageEdit_ie_left_image, 0);
                    if (resId != 0) {
                        iv_left.setImageResource(resId);
                    }
                    break;
                case R.styleable.ImageEdit_ie_right_hint:
                    resId = typedArray.getResourceId(R.styleable.ImageEdit_ie_right_hint, 0);
                    if (resId != 0) {
                        et_right.setHint(typedArray.getResources().getString(resId));
                    } else {
                        et_right.setHint(typedArray.getString(R.styleable.ImageEdit_ie_right_hint));
                    }
                    break;
            }
        }
        typedArray.recycle();
    }

    private void init() {
        mInflater = LayoutInflater.from(getContext());
        mInflater.inflate(R.layout.item_imageedit, this);
        iv_left = (ImageView) findViewById(R.id.iv_left);
        et_right = (EditText) findViewById(R.id.et_right);
        ViewGroup.LayoutParams param = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        setLayoutParams(param);
    }

    public String getValue() {
        if (et_right != null) {
            return et_right.getText().toString();
        }
        return null;
    }

    ;
}
